## 此工具用于从CardDefs.xml生成_carddb.txt文件
## 2019.09.15添加提取CardID及CardName的新功能

from xml.dom import minidom
import re

def InputCardDefsXml(FileName):
	with open (FileName, 'r', encoding = 'utf-8') as xml:
		dom = minidom.parse(xml)
		root = dom.documentElement
	return root

def OnlyElementNodes(nodeList):
	nodeList_new = []
	for i in range(len(nodeList)):
		if (nodeList[i].nodeType == 1):
			nodeList_new.append(nodeList[i])
	return nodeList_new

def GetCardDef(root):
	Entity = []
	Entitys = root.childNodes
	Entitys = OnlyElementNodes(Entitys)
	for j in range(len(Entitys)):
		version = Entitys[j].getAttribute('version')
		CardID = Entitys[j].getAttribute('CardID')
		Entity.append('  <Entity version="%s" CardID="%s">\n' %(version, CardID))
		Tags = Entitys[j].childNodes
		Tags = OnlyElementNodes(Tags)
		for i in range(len(Tags)):
			if(Tags[i].nodeName == 'Tag'):
				enumID = Tags[i].getAttribute('enumID')
				if(enumID == '185' or enumID == '184'):
					zhCN = Tags[i].getElementsByTagName('zhCN')
					enUS = Tags[i].getElementsByTagName('enUS')
					#LocString = zhCN[0].firstChild.data
					LocString = enUS[0].firstChild.data
					##去除特殊符号
					LocString = re.sub(r'\W', ' ', LocString)
					Entity.append('    <Tag enumID="%s" type="String">%s</Tag>\n' %(enumID, LocString))
				else:
					tagType = Tags[i].getAttribute('type')
					if(tagType == 'Int'):
						tagValue = Tags[i].getAttribute('value')
						Entity.append('    <Tag enumID="%s" value="%s" />\n' %(enumID, tagValue))
			if(Tags[i].nodeName == 'Power'):
				PlayRequirement = Tags[i].childNodes
				PlayRequirement = OnlyElementNodes(PlayRequirement)
				for k in range(len(PlayRequirement)):
					nodeName = PlayRequirement[k].nodeName
					param = PlayRequirement[k].getAttribute('param')
					reqID = PlayRequirement[k].getAttribute('reqID')
					powerStr = ('      <%s reqID="%s" param="%s" />\n' %(nodeName, reqID, param))
					Entity.append(powerStr)
		Entity.append('  </Entity>\n')
	return Entity

def GetCardID(root):
	cardIDs = []
	cradNames = []
	Entitys = root.childNodes
	Entitys = OnlyElementNodes(Entitys)
	## 添加.cs的格式
	cardIDs.append('namespace SilverFish.Enums\n{\n\tpublic enum CardIdEnum\n\t{\n')
	#cradNames.append('namespace SilverFish.Enums\n{\n\tpublic enum CardName\n\t{\n')
	for each in Entitys:
		cardID = each.getAttribute('CardID')
		cardIDs.append('\t\t' + cardID + ',\n')
		Tags = each.childNodes
		Tags = OnlyElementNodes(Tags)
		for i in range(len(Tags)):
			enumID = Tags[i].getAttribute('enumID')
			if(enumID == '185'):
				zhCN = Tags[i].getElementsByTagName('zhCN')
				enUS = Tags[i].getElementsByTagName('enUS')
				#LocString = zhCN[0].firstChild.data
				LocString = enUS[0].firstChild.data
				##去除特殊符号
				LocString = re.sub(r'\W', '', LocString)
				if (LocString == ''):
					continue
				cradNames.append('\t\t' + LocString + ',\n')
	cardIDs.append('\t}\n}')
	##去重复并重新排序
	cradNames = list(set(cradNames))
	cradNames.sort()
	cradNames.insert(0, 'namespace SilverFish.Enums\n{\n\tpublic enum CardName\n\t{\n')
	cradNames.insert(len(cradNames), '\t}\n}')
	return cardIDs, cradNames

def WriteTo_carddb(Entity):
	with open('../Result/_carddb.txt', 'w', encoding = 'utf-8') as carddb:
		carddb.write('<CardDefs>\n')
		carddb.writelines(Entity)
		carddb.write('</CardDefs>\n')
	return

def WriteTo_CardIDs(CardIDs_data, cradNames_data):
	with open('../Result/CardIdEnum.cs', 'w', encoding = 'utf-8') as cardIDs:
		cardIDs.writelines(CardIDs_data)
	with open('../Result/CardName.cs', 'w', encoding = 'utf-8') as cardNames:
		cardNames.writelines(cradNames_data)
	return


if __name__ == '__main__':
	fileName = '../Source/CardDefs.xml'
	print('Working...')
	root = InputCardDefsXml(fileName)
	Entity = GetCardDef(root)
	(cradIDs, cardNames) = GetCardID(root)
	WriteTo_carddb(Entity)
	WriteTo_CardIDs(cradIDs, cardNames)
	print('Done!')